<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_menus
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Base controller class for Menu Manager.
 *
 * @package     Joomla.Administrator
 * @subpackage  com_menus
 * @since       1.6
 */
class MenusController extends JControllerLegacy
{
	/**
	 * Method to display a view.
	 *
	 * @param   boolean			If true, the view output will be cached
	 * @param   array  An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
	 *
	 * @return  JController		This object to support chaining.
	 * @since   1.5
	 */
	public function display($cachable = false, $urlparams = false)
	{
		require_once JPATH_COMPONENT.'/helpers/menus.php';

		$view   = $this->input->get('view', 'menus');
		$layout = $this->input->get('layout', 'default');
		$id     = $this->input->getInt('id');

		// Check for edit form.
		if ($view == 'menu' && $layout == 'edit' && !$this->checkEditId('com_usermenus.edit.menu', $id)) {

			// Somehow the person just went to the form - we don't allow that.
			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=com_usermenus&view=items', false));

			return false;
		}
		elseif ($view == 'item' && $layout == 'edit' && !$this->checkEditId('com_usermenus.edit.item', $id)) {

			// Somehow the person just went to the form - we don't allow that.
			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=com_usermenus&view=items', false));

			return false;
		}

		parent::display();

		return $this;
	}
	
	function copymainmenu(){
		
		$user   = & JFactory::getUser();
	    $uid  	= $user->get('id');
		// Get a db connection.
		$db = JFactory::getDbo();
		// Create a new query object.
		$query = $db->getQuery(true);
		// Select all records from the user profile table where key begins with "custom.".
		// Order it by the ordering field.
		$type = 'mainmenu';
		$query->select('*');
		$query->from('#__menu');
		$query->where('menutype='.$db->quote($type));
		// Reset the query using our newly populated query object.
		$db->setQuery($query);
		$results = $db->loadObjectList();
		
		//check to delete user_menu if exist
			$query_de = "delete from #__usermenu where user_id = $uid";
			$db->setQuery($query_de);
		    $db->execute();
		//end check 
		foreach($results as $row){
			$id 			   		= $row -> id;
			$menutype 		 	  = 'usermenus';
			$title 		  		 = $row -> title;
			$alias				 = $row -> alias;
			$note				  = $row -> note;
			$path			 	  = $row -> path;
			$link			 	  = $row -> link;
			$type 		     	  = $row -> type;
			$published 			 = 0;
			$parent_id 			 = 0;
			$level 		    	 = $row -> level;
			$component_id     	  = $row -> component_id;
			$checked_out	  	   = $row -> checked_out;
			$checked_out_time	  = $row -> checked_out_time;
			$browseNav		     = $row -> browserNav;
			$access				= $row -> access;
			$img				   = $row -> img;
			$template_style_id     = $row -> template_style_id;
			$params				= $row -> params;
			$lft				   = $row -> lft;
			$rgt				   = $row -> rgt;
			$home				  = $row -> home;
			$language			  = $row -> language;
			$client_id 			 = $row -> client_id;
			
			$query_in 		= "insert into #__usermenu(menutype,title,alias,note,path,link,type,published,parent_id,level,component_id,
												 checked_out, checked_out_time, browserNav, access,img,template_style_id, params,
												 lft,rgt,home,language,client_id,user_id)
						 	    values('$menutype','$title','$alias','$note','$path','$link', '$type',
									 '$published', '$parent_id', '$level', '$component_id', '$checked_out',
									 '$checked_out_time', '$browseNav', '$access', '$img', '$template_style_id',
									 '$params', '$lft', '$rgt', '$home','$language', '$client_id', $uid )";
			$db->setQuery($query_in);
		    $db->execute();
			//echo $query_in."<br/>";
			// Reset the query using our newly populated query object.
		}
		//die;
		//$this->setRedirect('index.php?option=com_usermenu&view=items',false);
		$str = "index.php?option=com_usermenus&view=items";
		header("Location:$str");
	}
}
